<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends MY_Controller {

	function __construct() {
		parent::__construct();
	}

	function index() {
    	$username = $this->input->post("username");
        $password = $this->input->post("password");

    	$validate_data['username'] = $username;
    	if (!$this->_ajax_validate($validate_data)) { return; }
    	
     	$validate_data['password'] = $password;
    	if (!$this->_ajax_validate($validate_data)) { return; }
    	
        $this->load->model('u_user', 'user_m');
        
        $data = $this->user_m->get_by('username', $username, '*', TRUE);
        
        if ($data) {
            if ($password != $data['password']) {
                $this->output->set_output('password|data|error');
            } else {
            	/* PHP SESSION */
            	$_SESSION['uid'] = $data['id'];
            	$_SESSION['username'] = $data['username'];
                $_SESSION['ucid'] = $data['category'];
                $_SESSION['name'] = $data['name'];
                
                /* CI SESSION */
                /* sess_match_useragent 要设置为 FALSE, 否则在自定义 session 后刷新会调 session */
                $this->session->set_userdata($_SESSION);
                
                $this->output->set_output('succeed');
            }
        } else {
	        $this->output->set_output('username|data|error');
        }
    }
    
	function logout() {
		$this->session->sess_destroy();
		session_destroy();
		
		$this->output->set_output('succeed');
	}
}